{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Where the usual graphcut constructs a graph out of the image's voxels, the label graphcut used irregular regions instead. This reduces memory consumption, accelerates the execution and leads mostly to better results.\n",
    "\n",
    "<table>\n",
    "<tr>\n",
    "<td><img alt=\"\" src=\"images/b0.png\" width=\"200\" /></td>\n",
    "<td><img alt=\"\" src=\"images/b0wmarker.png\" width=\"200\" /></td>\n",
    "</tr>\n",
    "<tr>\n",
    "<td>Original image</td>\n",
    "<td>Foreground (red) and background (green) marker on original image</td>\n",
    "</tr>\n",
    "</table>\n",
    "\n",
    "Let's assume we want to segment the ventricles in a brain scan (left image). First, we need some markers for foreground (red) and background (green). These can be seen in the right image and are usually either created manually or by another algorithm."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Creating the region image\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The label graphcut requires a region image. **MedPy** supplies the required functionalities. First, we create a gradient magnitude version of our original image with"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "medpy_gradient.py resources/b0.nii.gz output/gradient.nii.gz"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Which we then feed to the watershed algorith"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "medpy_watershed.py output/gradient.nii.gz output/watershed.nii.gz --mindist 10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Visually, the result process like this\n",
    "<table>\n",
    "<tr>\n",
    "<td><img alt=\"Original image\" src=\"images/b0.png\" width=\"200\" /></td>\n",
    "<td><img alt=\"Region image\" src=\"images/b0gradient.png\" width=\"200\" /></td>\n",
    "<td><img alt=\"Region image\" src=\"images/watershed.png\" width=\"200\" /></td>\n",
    "<td><img alt=\"Region image (colored)\" src=\"images/watershed_colored.png\" width=\"200\" /></td>\n",
    "</tr>\n",
    "<tr>\n",
    "<td>Original image</td>\n",
    "<td>Gradient image</td>\n",
    "<td>Region image</td>\n",
    "<td>Region image (colored)</td>\n",
    "</tr>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Performing the graphcut"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we just have to supply our images to the script"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "medpy_graphcut_label.py output/gradient.nii.gz output/watershed.nii.gz resources/b0markers.nii.gz output/graphcut_label.nii.gz --boundary=stawiaski"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And, after some waiting time, we obtain\n",
    "<img alt=\"\" src=\"images/b0gclabelstawiaski.png\" width=\"300\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ", which is a pretty good approximation of the ventricles visible in the brain scan."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Bash",
   "language": "bash",
   "name": "bash"
  },
  "language_info": {
   "codemirror_mode": "shell",
   "file_extension": ".sh",
   "mimetype": "text/x-sh",
   "name": "bash"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
